package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/cache"
	"github.com/astaxie/beego/utils/captcha"
	"log"
)

type LoginController struct {
	beego.Controller
}

var cpt *captcha.Captcha

func init() {
	store := cache.NewMemoryCache()
	cpt = captcha.NewWithFilter("/captcha/", store)
	cpt.ChallengeNums = 4
	cpt.StdWidth = 150
	cpt.StdHeight = 50
	cpt.FieldIDName = "captcha_id"
	cpt.FieldCaptchaName = "captcha"
}

func (c *LoginController) Get() {

	c.TplName = "login1.html"

}

func (c *LoginController) Post() {
	op:=c.Input().Get("op")
	log.Println("op>",op)
	if op=="logout"{
		c.DelSession("uid")
		return
	}
	
	c.Data["Success"] = cpt.VerifyReq(c.Ctx.Request)
	log.Println("verify", c.Data["Success"])

	uname:=c.Input().Get("uname")
	upass:=c.Input().Get("upass")
	log.Println("uname:",uname,"  upass:",upass)

	islogin:=0
	if c.Data["Success"]==false {
		islogin=1

	}else if uname!="admin"{
		islogin=2
	}else if upass!="admin"{
		islogin=3
	}else{
		log.Println("写入uid到ctx中")
		//c.Ctx.Input.CruSession.Set("uid",1)
		c.SetSession("uid",1)
	}

	c.Data["json"] = map[string]interface{}{"islogin": islogin, "message": "111"}
	c.ServeJSON()
	// 	c.TplName = "login.html"
}
